import unittest
import requests


class DianShang(unittest.TestCase):
    def setUp(self) -> None:
        print('开始')

    def tearDown(self) -> None:
        print('结束')

    host = 'http://10.59.9.18/'
    # 登录
    sessionId = None
    userId = None
    nickName = None

    def test_DianShang_001(self):
        login = requests.post(url=DianShang.host + 'small/user/v1/login',
                              json={'phone': '15385256987', 'pwd': '123456Aa'})
        print(login.json())
        # 提取响应中的sessionId、userId
        DianShang.sessionId = str(login.json()['result']['sessionId'])
        DianShang.userId = str(login.json()['result']['userId'])
        # 断言 提取nickName 丁乐二
        DianShang.nickName = login.json()['result']['nickName']
        self.assertEqual('丁乐二', DianShang.nickName)

    commodityId = None
    commodityName = None

    # 首页商品信息列表
    def test_DianShang_002(self):
        dityList = requests.get(url=DianShang.host + 'small/commodity/v1/commodityList')
        print(dityList.json())
        # 提取响应中的commodityId
        DianShang.commodityId = dityList.json()['result']['rxxp']['commodityList'][0]['commodityId']
        print('commodityId', DianShang.commodityId)

    # 商品详情
    categoryName = None

    def test_DianShang_003(self):
        dityList = requests.get(url=DianShang.host + 'small/commodity/v1/findCommodityDetailsById',
                                headers={'userId': DianShang.userId, 'sessionId': DianShang.sessionId},
                                params={'commodityId': DianShang.commodityId})
        print(dityList.json())
        # 断言 提取categoryName 女鞋
        DianShang.categoryName = dityList.json()['result']['categoryName']
        self.assertTrue('女鞋' == DianShang.categoryName)

    address_id = None
    addressName = None

    # 收货地址列表
    def test_DianShang_004(self):
        AddressList = requests.get(url=DianShang.host + 'small/user/verify/v1/receiveAddressList',
                                   headers={'userId': DianShang.userId, 'sessionId': DianShang.sessionId})
        print(AddressList.json())
        # 提取地址id
        DianShang.address_id = AddressList.json()['result'][0]['id']
        print('地址id:', DianShang.address_id)
        # 断言 提取address_id 北京 八维
        DianShang.addressName = AddressList.json()['result'][0]['address']
        self.assertEqual('北京 八维', DianShang.addressName)

    # 根据订单状态查询订单信息
    orderId=None
    def test_DianShang_005(self):
        ByStatus = requests.get(url=DianShang.host + 'small/order/verify/v1/findOrderListByStatus',
                                 headers={'userId': DianShang.userId, 'sessionId': DianShang.sessionId},
                                 params={'status': 0, 'page': 1, 'count': 5})
        print(ByStatus.json())
        # 提取订单id
        DianShang.orderId=ByStatus.json()['orderList'][0]['orderId']
        print(DianShang.orderId)

    # 创建订单

    def test_DianShang_006(self):
        createOrder = requests.post(url=DianShang.host + 'small/order/verify/v1/createOrder',
                                    headers={'userId': DianShang.userId, 'sessionId': DianShang.sessionId},
                                    params={
                                        'orderInfo': [{"commodityId": DianShang.commodityId, "amount": 1}, {"commodityId": 5, "amount": 1}],
                                        'totalPrice': '2.00', 'addressId': str(DianShang.address_id)})
        print(createOrder.json())

    # 支付
    def test_DianShang_007(self):
        createOrder = requests.post(url=DianShang.host + 'small/order/verify/v1/pay',
                                    headers={'userId': DianShang.userId, 'sessionId': DianShang.sessionId},
                                    data={'orderId': DianShang.orderId, 'payType': 1})
        print(createOrder.json())


if __name__ == '__main__':
    unittest.main()
